//package ynu.lcy.elememicroservicecommon.aop;
//
//
//import eleme.lcy.elemecloud.common.annotation.AuthCheck;
//import eleme.lcy.elemecloud.common.enums.UserRoleEnum;
//import eleme.lcy.elemecloud.common.exception.BusinessException;
//import eleme.lcy.elemecloud.common.utils.ErrorCode;
//import eleme.lcy.elemecloud.common.utils.TokenUtils;
//import eleme.lcy.elemecloud.service.UserService;
//import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//
//import static eleme.lcy.elemecloud.common.constant.CommonConstant.ACCESS_TOKEN;
//
//
///**
// * 权限校验 AOP
// */
//@Aspect
//@Component
//public class AuthInterceptor {
//
//    @Resource
//    private UserService userService;
//
//    @Resource
//    private TokenUtils tokenUtils;
//
//    /**
//     * 执行拦截
//     *
//     * @param joinPoint
//     * @param authCheck
//     * @return
//     */
//    @Around("@annotation(authCheck)")
//    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
//        String mustRole = authCheck.mustRole();
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        // 当前登录用户
//        String userRole = tokenUtils.getCurrentUserRole(request, ACCESS_TOKEN);
//        // 必须有该权限才通过
//        if (StringUtils.isNotBlank(mustRole)) {
//            UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValue(mustRole);
//            if (mustUserRoleEnum == null) {
//                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//            }
//            // 如果被封号，直接拒绝
//            if (UserRoleEnum.BAN.equals(mustUserRoleEnum)) {
//                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//            }
//            // 必须有管理员权限
//            if (UserRoleEnum.ADMIN.equals(mustUserRoleEnum)) {
//                if (!mustRole.equals(userRole)) {
//                    throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//                }
//            }
//        }
//        // 通过权限校验，放行
//        return joinPoint.proceed();
//    }
//}
//
